<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Common_user_manage extends CI_Controller {


	public function __construct() {
		parent::__construct();
		session_start();
		$this->load->library("common/login_manage");
		$this->load->library("common/right_manage");
		$this->load->library("common/user_manage");
		$this->load->library("common/user_menu");
		$this->config->load("config_common");
	}

	public function index() {
		$this->user_list();
	}
	public function to_add_user() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']=$this->user_menu->get_user_menu();
		if (!$this->right_manage->check_right("add_user",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}
		
		$this->load->library("common/dept");
		$data['top_depts']	= $this->dept->get_depts(0);

		$this->load->library("common/role_manage");
		$data['role_list']=$this->role_manage->query_all_role();
		$this->load->view("common/user_manage/to_add_user",$data);
	}

	public function check_user_name() {
		$user_name	=trim($this->input->get_post("user_name"));
		$user_id	=$this->input->get_post("user_id");
		$result		=$this->user_manage->check_user_name($user_name,$user_id);
		echo json_encode($result);
	}
	public function check_name() {
		$name		=trim($this->input->get_post("name"));
		$result		=$this->user_manage->check_name($name);
		echo json_encode($result);
	}
	public function check_email() {
		$email		=trim($this->input->get_post("email"));
		$result		=$this->user_manage->check_email($email);
		echo json_encode($result);
	}

	public function add_user() {
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}
		if (!$this->right_manage->check_right("add_user")) {
			$result['result']	=false;
			$result['message']	='你没有权限使用这个功能';
			echo json_encode($result);die();
		}
		$data['user_name']	=trim($this->input->get_post("user_name"));
		$data['password']	=trim($this->input->get_post("password"));
		$data['name']		=trim($this->input->get_post("name"));
		$data['role_id']	=$this->input->get_post("role_id");
		$data['dept_id']	=trim($this->input->get_post("dept_id"));
		$data['dept_id1']	=trim($this->input->get_post("dept_id1"));
		$data['dept_id2']	=trim($this->input->get_post("dept_id2"));
		$data['email']		=trim($this->input->get_post("email"));
		$data['sex']		=trim($this->input->get_post("sex"));
		$result	=$this->user_manage->add_user($data);
		echo json_encode($result);
	}

	public function add_user_end() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']=$this->user_menu->get_user_menu();
		$user_id	=$this->input->get("user_id");
		$user_info	=$this->user_manage->get_user_info($user_id);
		$data['title']	="新增用户成功";
		$data['message']="新增用户 <strong>".$user_info['NAME']."(".$user_info['USER_NAME'].")</strong> 成功!";
		$data['back_url']=base_url()."index.php/common_user_manage";
		$this->load->view("common/success_message",$data);
	}

	public function user_list() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']=$this->user_menu->get_user_menu();
		
		
		$data['update_user']	=false;
		if ($this->right_manage->check_right("update_user")) {
			$data['update_user']	=true;
		}
		$data['delete_user']	=false;
		if ($this->right_manage->check_right("delete_user")) {
			$data['delete_user']=true;
		}
		$data['add_user']	=false;
		if ($this->right_manage->check_right("add_user")) {
			$data['add_user']	=true;
		}
		$data['query_user']	=false;
		if ($this->right_manage->check_right("query_user")) {
			$data['query_user']	=true;
		}
		$data['assign_user_area']= false;
		if ($this->right_manage->check_right("assign_user_area")) {
			$data['assign_user_area']	=true;
		}

		$data['user_list'] 	=$this->user_manage->query_user_list();
		$data['user_num']	=$this->user_manage->query_user_num();
		$this->load->library("common/pagination");
		$data['page_link']	=$this->pagination->get_pagination(base_url()."index.php/common_user_manage/user_list?",$data['user_num']);
		$this->load->view("common/user_manage/user_list",$data);
	}

	public function query_user_detail() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']		=$this->user_menu->get_user_menu();
		if (!$this->right_manage->check_right("query_user")) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}
		$data['user_update']	=false;
		if ($this->right_manage->check_right("user_update")) {
			$data['user_update']	=true;
		}

		$user_id				=$this->input->get("user_id");
		$data['user_detail'] 	=$this->user_manage->query_user_detail($user_id);
		if ($data['user_detail']==null) {
			$data['title']	="查看用户失败";
			$data['message']="你选择的用户不存在!";
			$data['back_url']=base_url()."index.php/common_user_manage/user_list";
			$this->load->view("common/error_tip",$data);
			return ;
		}

		$data['user_role_list']=$this->role_manage->query_user_role($user_id);
		$this->load->view("common/user_manage/query_user_detail",$data);
	}

	public function to_modify_user() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']=$this->user_menu->get_user_menu();
		if (!$this->right_manage->check_right("update_user")) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$this->load->library("common/role_manage");
		$data['role_list']=$this->role_manage->query_all_role();
		$user_id		=$this->input->get("user_id");
		
		$data['user_role_list']=$this->role_manage->query_user_role($user_id);
		if ($user_id==1) {
			$data['title']	="编辑失败";
			$data['message']="你不能编辑管理员!";
			$data['back_url']=base_url()."index.php/common_user_manage/user_list";
			$this->load->view("common/error_tip",$data);
			return ;
		}
		$data['user_detail'] 	=$this->user_manage->query_user_detail($user_id);
		if ($data['user_detail']==null) {
			$data['title']	="编辑用户失败";
			$data['message']="你选择的用户不存在!";
			$data['back_url']=base_url()."index.php/common_user_manage/user_list";
			$this->load->view("common/error_tip",$data);
			return ;
		}
		
		$this->load->library("common/dept");
		$data['top_depts']	= $this->dept->get_depts(0);
		$data['second_depts'] =null;
		$data['last_depts'] =null;
		if($data['user_detail']['dept_id1']){
			$data['second_depts'] =$this->dept->get_depts($data['user_detail']['dept_id']);
		
		}
		if($data['user_detail']['dept_id2']){
			$data['last_depts'] =$this->dept->get_depts($data['user_detail']['dept_id1']);
		
		}
		
		$this->load->view("common/user_manage/to_modify_user",$data);
	}

	public function modify_user(){
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}
		if (!$this->right_manage->check_right("update_user")) {
			$result['result']	=false;
			$result['message']	='你没有权限使用这个功能';
			echo json_encode($result);die();
		}
		$data['user_name']	=trim($this->input->get_post("user_name"));
		$data['name']		=trim($this->input->get_post("name"));
		$data['role_id']	=$this->input->get_post("role_id");
		$data['dept_id']	=trim($this->input->get_post("dept_id"));
		$data['dept_id1']	=trim($this->input->get_post("dept_id1"));
		$data['dept_id2']	=trim($this->input->get_post("dept_id2"));
		$data['email']		=trim($this->input->get_post("email"));
		$data['sex']		=trim($this->input->get_post("sex"));
		$user_id			=$this->input->get_post("user_id");
		$result	=$this->user_manage->modify_user($data,$user_id);
		echo json_encode($result);
	}

	public function modify_user_end() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']=$this->user_menu->get_user_menu();
		$user_id	=$this->input->get("user_id");
		$user_info	=$this->user_manage->get_user_info($user_id);
		$data['title']	="编辑用户成功";
		$data['message']="编辑用户 <strong>".$user_info['NAME']."(".$user_info['USER_NAME'].")</strong> 成功!";
		$data['back_url']=base_url()."index.php/common_user_manage/user_list";
		$this->load->view("common/success_message",$data);
	}

	public function to_delete_user() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']=$this->user_menu->get_user_menu();
		if (!$this->right_manage->check_right("user_delete")) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$data['back_url']	=base_url()."index.php/common_right_manage";
			$this->load->view("common/error_tip",$data);
			return;
		}


		$user_id	=$this->input->get("user_id");

		if ($user_id==1) {
			$data['title']	="删除用户失败";
			$data['message']="你不能删除管理员!";
			$data['back_url']=base_url()."index.php/common_user_manage/user_list";
			$this->load->view("common/error_tip",$data);
			return ;
		}
		$user_info	=$this->user_manage->get_user_info($user_id);
		if ($user_info==null) {
			$data['title']		="删除用户失败";
			$data['message']	="你要删除的用户不存在";
			$data['back_url']	=base_url()."index.php/common_user_manage/user_list";
			$this->load->view("common/error_tip",$data);
			return ;
		}


		$data['title']			="确认删除用户?";
		$data['message']		="是否确认要删除用户  <strong>".$user_info['NAME']."(".$user_info['USER_NAME'].")</strong> ?";
		$data['back_url']		=base_url()."index.php/common_user_manage/user_list";
		$data['forward_url']	=base_url()."index.php/common_user_manage/delete_user?user_id=".$user_id;
		$this->load->view("common/warning_message",$data);
		return ;

	}

	public function delete_user() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']=$this->user_menu->get_user_menu();
		if (!$this->right_manage->check_right("delete_user")) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$user_id	=$this->input->get("user_id");
		if ($user_id==1) {
			$data['title']	="删除用户失败";
			$data['message']="你不能删除管理员!";
			$data['back_url']=base_url()."index.php/common_user_manage/user_list";
			$this->load->view("common/error_tip",$data);
			return ;
		}
		$user_info	=$this->user_manage->get_user_info($user_id);
		if ($user_info==null) {
			$data['title']		="删除用户失败";
			$data['message']	="你要删除的用户不存在";
			$data['back_url']	=base_url()."index.php/common_user_manage/user_list";
			$this->load->view("common/error_tip",$data);
			return ;
		}
		$this->user_manage->delete_user($user_id);
		echo "<script>history.go(-1)</script>";
	}

	public function search_user() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']		=$this->user_menu->get_user_menu();
		$data['user_update']	=false;
		if ($this->right_manage->check_right("user_update")) {
			$data['user_update']	=true;
		}
		$data['user_delete']	=false;
		if ($this->right_manage->check_right("user_delete")) {
			$data['user_delete']	=true;
		}
		$data['key'] 			=$this->input->get("key");
		if ($data['key']==null) {
			$data['user_list']	=null;
			$data['user_num']	=0;
		}else{
			$data['user_list']	=$this->user_manage->search_user($data['key']);
			$data['user_num']	=count($data['user_list']);
		}
		$this->load->view("common/user_manage/search_user",$data);
	}
	
	public function to_modify_user_area(){
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']=$this->user_menu->get_user_menu();
		if (!$this->right_manage->check_right("assign_user_area")) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$user_id		=$this->input->get("user_id");
		
// 		if ($user_id==1) {
// 			$data['title']	="编辑失败";
// 			$data['message']="你不能编辑管理员!";
// 			$data['back_url']=base_url()."index.php/common_user_manage/user_list";
// 			$this->load->view("common/error_tip",$data);
// 			return ;
// 		}
		$data['user_detail'] 	=$this->user_manage->query_user_detail($user_id);
		$data['user_area_list'] =$this->user_manage->query_user_area($user_id);
		if ($data['user_detail']==null) {
			$data['title']	="编辑用户失败";
			$data['message']="你选择的用户不存在!";
			$data['back_url']=base_url()."index.php/common_user_manage/user_list";
			$this->load->view("common/error_tip",$data);
			return ;
		}
		
		$this->load->library("sys/region");
		$data['top_regions']	= $this->region->get_regions(0);
		
		//到市级的用户区域 暂时屏蔽
// 		$data['second_regions']=array();
// 		foreach ($data['top_regions'] as $parent){
// 			array_push($data['second_regions'],array($parent['region_id'],$this->region->get_regions($parent['region_id'])));
// 		}
		$this->load->view("common/user_manage/to_modify_user_area",$data);
	}
	
	public function modify_user_area(){
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}
		if (!$this->right_manage->check_right("assign_user_area")) {
			$result['result']	=false;
			$result['message']	='你没有权限使用这个功能';
			echo json_encode($result);die();
		}
		$region_id	=$this->input->post("region_id");
		$user_id	=$this->input->post("user_id");
		$result	=$this->user_manage->modify_user_area($user_id,$region_id);
		echo json_encode($result);
	}
}